package com.xatu.yl.storeshopping.activity.RegisterAndLogin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.namespace.R;

import android.widget.RadioGroup;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xatu.yl.storeshopping.activity.ShopperMainActivity;
import com.xatu.yl.storeshopping.activity.UserMainActivity;
import com.xatu.yl.storeshopping.service.GpsService;
import com.xatu.yl.storeshopping.util.ToastUtil;
import com.xatu.yl.util.DesUtil2;
import com.xatu.yl.storeshopping.AppContent;

import org.apache.http.entity.ByteArrayEntityHC4;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;

public class LoginActivity extends Activity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private Button reg_shopper_btn,reg_user_btn;
    private EditText id;
    private EditText pass;
    private RadioGroup role_group;
    private String role = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //开启GPS获取服务，这里修改，应为在登录的时候需要获取GPS信息，所以需要提前开启
        Intent i = new Intent(this, GpsService.class);
        startService(i);
        reg_shopper_btn=this.findViewById(R.id.reg_shopper_btn);
        reg_user_btn=this.findViewById(R.id.reg_user_btn);
        id=this.findViewById(R.id.usr_id);
        pass=this.findViewById(R.id.usr_pass);
        role_group=findViewById(R.id.role_group);
        role_group.setOnCheckedChangeListener(this);
        reg_user_btn.setOnClickListener(this);
        reg_shopper_btn.setOnClickListener(this);
    }



    //给按钮设置一个点击事件的方法：login
    public void login(View view) {
        if (role.equals("")){
            new ToastUtil(getApplicationContext()).showToast("请先选择角色",R.drawable.warning,R.color.warning,Toast.LENGTH_SHORT);
        }else{
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            //将参数添加到params中
            params.put("userId",id.getText().toString());
            params.put("pwd",pass.getText().toString());
            params.put("role",role);

            client.get(AppContent.baseUrl+AppContent.loginUrl,params,loginHandler);
        }
    }


    public JsonHttpResponseHandler loginHandler=new JsonHttpResponseHandler(){

        @Override
        public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONObject response)
        {
            String status = "";
            String result = "";
            String errorMessage = "";
            try {
                status = response.getString("status");
                result = response.getString("result");
                //获取登录之后的唯一标识
                AppContent.token=response.getString("token");
                errorMessage = response.getString("errorMessage");
            } catch (JSONException e) {
                new ToastUtil(getApplicationContext()).showToast("数据解析错误",R.drawable.error,R.color.error,Toast.LENGTH_SHORT);
            }
            if (!(status.equals("200") && result.equals("已通过"))){
                new ToastUtil(getApplicationContext()).showToast(errorMessage,R.drawable.error,R.color.error,Toast.LENGTH_SHORT);
            }else {
                Intent intent = null;
                new ToastUtil(getApplicationContext()).showToast(role+"登陆成功，欢迎您！",R.drawable.success,R.color.success,Toast.LENGTH_SHORT);
                if(role.equals("user")){
                    intent = new Intent(LoginActivity.this, UserMainActivity.class);
                }
                if (role.equals("shopper")){
                    intent = new Intent(LoginActivity.this, ShopperMainActivity.class);
                }
                startActivity(intent);
                finish();
            }
        }

        @Override
        public void onFailure(int statusCode, org.apache.http.Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            new ToastUtil(getApplicationContext()).showToast("登录失败！",R.drawable.error,R.color.error,Toast.LENGTH_SHORT);
        }
    };


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(checkedId == R.id.user_radiobtn){
            role = "user";
        }
        if(checkedId == R.id.shopper_radiobtn){
            role = "shopper";
        }
    }

    @Override
    public void onClick(View v) {
        //商家注册
        if(v.getId()==R.id.reg_shopper_btn){
           startActivity(new Intent(LoginActivity.this, UserRegisterActivity.class));
        }
        //用户注册
        if(v.getId()==R.id.reg_user_btn){
            startActivity(new Intent(LoginActivity.this, ShopperRegisterActivity.class));
        }
    }


}